<template>
  <t-dialog :header="row ? `修改学习计划-${row.planName}` : '新增学习计划'" :visible.sync="visible" :onClose="closeDialog"
    :closeOnOverlayClick="false" :onConfirm="onClick" width="40%" top="5%">
    <div style="height: 50vh; width: 100%; display: flex">
      <t-form :data="formData" labelWidth="100px" style="margin: auto;width: 100%;">
        <t-form-item label="计划名称" name="planName">
          <t-input v-model="formData.planName" clearable placeholder="请输入学习计划名称" />
        </t-form-item>
        <t-form-item label="计划日期" name="planDate">
          <t-date-picker v-model="formData.planDate" />
        </t-form-item>
        <t-form-item label="地点" name="planPlace">
          <t-input v-model="formData.planPlace" clearable placeholder="请输入地点" />
        </t-form-item>
        <t-form-item label="内容" name="planContent">
          <t-textarea v-model="formData.planContent" :autosize="{ minRows: 5 }" placeholder="请输入内容" />
        </t-form-item>
        <t-form-item label="附件" name="sysAttMainVo">
          <file-upload v-model="formData.sysAttMainVo.sysAttMainIds" :moduleKey="formData.moduleKey"
            :fileList.sync="formData.fileLists" :multiple="true">
            <t-button variant="outline">
              <t-icon name="cloud-upload" slot="icon" />
              点击上传
            </t-button>
          </file-upload>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from '@/components/FileUpload/index.vue';
import { getFiles } from '@/api/common';

export default {
  components: {
    FileUpload,
  },
  dicts: ['materials_format'],
  props: {
    objId: [String, Number],
    header: String,
    row: {
      type: Object,
      default: null,
    },
  },
  data () {
    return {
      visible: true,
      formData: {
        planName: this.row?.planName,
        planDate: this.row?.planDate,
        planPlace: this.row?.planPlace,
        moduleKey: 'zjkxxjh',
        fileLists: [],
        planContent: this.row?.planContent,
        sysAttMainVo: {
          sysAttMainIds: [],
        },
      },
    };
  },
  created () {
    if (this.row) {
      getFiles(this.row.id)
        .then((res) => {
          res.data.forEach((item) => {
            this.formData.fileLists.push({
              name: item.fileName,
              url: item.fullFilePath,
              id: item.id,
              filePath: item.filePath,
            });
          });
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    }
  },
  methods: {
    closeDialog () {
      this.$emit('update:visible', false);
    },
    onClick () {
      console.log('submit');
      if (this.row) {
        this.$api.zjk.learning
          .updateLearningPlan({ ...this.formData, id: this.row.id })
          .then((res) => {
            console.log(res);
            this.$message.success('编辑成功');
            this.closeDialog();
            this.$emit('reload');
          })
          .catch((e) => {
            this.$message.error(e.toString());
          });
      } else {
        this.$api.zjk.learning
          .addLearningPlan(this.formData)
          .then((res) => {
            console.log(res);
            this.closeDialog();
            this.$message.success('新增成功');
            this.$emit('reload');
          })
          .catch((e) => {
            this.$message.error(e.toString());
          });
      }
    },
  },
};
</script>
